﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using RushTracker.Core.Util;
using RushTracker.Core.Data;
using RushTracker.Core.Models.Torrents;

namespace RushTracker.Web.UI
{
    public static class UIHelper
    {
        public static string GetFileSizeAsString(long size)
        {
            double s = size;
            string[] format = new string[] { "{0} bytes", "{0} KB", "{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB", "{0} ZB", "{0} YB" };
            int i = 0;
            while (i < format.Length - 1 && s >= 1024)
            {
                s = (100 * s / 1024) / 100.0;
                i++;

            }
            return string.Format(format[i], s.ToString("###,###,##0.##"));
        }

        public static string GetIconFromFileName(string fileName)
        {
            string ext = System.IO.Path.GetExtension(fileName);

            // check if rar file
            if (ext.StartsWith(".r"))
            {
                string rarPart = ext.Substring(ext.IndexOf("r") + 1);
                int isRar = 0;

                if (Int32.TryParse(rarPart, out isRar))
                {
                    // rar file!
                    return VirtualPathUtility.ToAbsolute("~/img/winrar.png");
                }

                if (ext.Equals(".rar"))
                {
                    return VirtualPathUtility.ToAbsolute("~/img/winrar.png");
                }
            }

            return "";
        }

        public static IEnumerable<SelectListItem> GetMetaFieldDataTypes()
        {
            return GetMetaFieldDataTypes("0");
        }

        public static IEnumerable<SelectListItem> GetMetaFieldDataTypes(string selectedValue)
        {
            List<SelectListItem> items = new List<SelectListItem>();

            items.Add(new SelectListItem() { Value = "0", Text = "" });
            items.Add(new SelectListItem() { Text = "bool", Value = "System.Boolean" });
            items.Add(new SelectListItem() { Text = "int", Value = "System.Int32" });
            items.Add(new SelectListItem() { Text = "string", Value = "System.String" });

            foreach (SelectListItem sli in items)
            {
                if (sli.Value == selectedValue)
                    sli.Selected = true;
            }

            return items;
        }

        public static IEnumerable<SelectListItem> GetCategoriesAsItemList()
        {
            return GetCategoriesAsItemList("");
        }

        public static IEnumerable<SelectListItem> GetCategoriesAsItemList(string selectedValue)
        {
            IDataRepository data = IoC.Resolve<IDataRepository>();
            IList<Category> categories = data.GetTopLevelCategories();
            List<SelectListItem> items = new List<SelectListItem>();

            string depth = "";
            items.Add(new SelectListItem() { Text = "(top level category)", Value = "0" });

            foreach (Category c in categories.OrderBy(cat => cat.Name).ToList())
            {
                depth = "";

                items.Add(new SelectListItem() { Text = c.Name, Value = c.Id.ToString() });

                depth += " - ";
                AddChildren(items, c, depth);
            }

            SelectListItem sli = items.FirstOrDefault(s => s.Value == selectedValue);

            if (sli != null)
                sli.Selected = true;

            return items;
        }

        private static void AddChildren(List<SelectListItem> items, Category c, string depth)
        {
            string d = depth;

            foreach (Category cat in c.Categories)
            {
                depth = d;

                items.Add(new SelectListItem() { Text = depth + cat.Name, Value = cat.Id.ToString() });
                depth += "- ";

                AddChildren(items, cat, depth);
            }
        }

        public static IEnumerable<SelectListItem> GetMetaFieldsAsList()
        {
            IDataRepository data = IoC.Resolve<IDataRepository>();
            IList<MetaField> metafields = data.GetAll<MetaField>();
            List<SelectListItem> items = new List<SelectListItem>();

            foreach (MetaField mf in metafields)
            {
                items.Add(new SelectListItem() { Text = mf.Name, Value = mf.Id.ToString() });
            }

            return items;
        }
    }
}
